package com.ztgf.shopping.api.cms.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.shopping.models.bo.shop.ShopQualificationBO;
import com.ztgf.shopping.models.dto.shop.ShopQualificationDTO;
import com.ztgf.shopping.models.service.shop.IShopInfoService;
import com.ztgf.shopping.models.service.shop.IShopQualificationService;
import com.ztgf.shopping.models.vo.shop.ShopQualificationBrandVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 店铺资质表控制器
 *
 * @author zhanghualang
 * @Date 2020-05-28 14:40:54
 */

@RestController
@RequestMapping("/cms/shopQualification")
@Slf4j
@Api(value = "ShopQualificationController",tags = "店铺资质表接口")
public class ShopQualificationController {

    @DubboReference
    private IShopQualificationService shopQualificationService;
    @DubboReference
    private IShopInfoService shopInfoService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShopQualificationDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        ShopQualificationDTO dto = shopQualificationService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "店铺基础资质表列表",notes = "店铺基础资质表列表")
    @PostMapping("/queryBaseList")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<List<ShopQualificationDTO>> queryBaseList(@RequestParam("id") Long id){
        Wrapper<ShopQualificationDTO> wrapper = new EntityWrapper<>();

        wrapper.eq("shop_id", id);
        wrapper.andNew().eq("type", 0).or().eq("type", 1).or().eq("type", 2);
        List<ShopQualificationDTO> dtos = shopQualificationService.selectList(wrapper);

        return new Result<>(dtos);
    }

    @ApiOperation(value = "用户基础资质表列表",notes = "用户基础资质表列表")
    @PostMapping("/queryUserBaseList")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<List<ShopQualificationDTO>> queryUserBaseList(@RequestParam("id") Long id){
        Wrapper<ShopQualificationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_id", id);
        wrapper.andNew().eq("type", 0).or().eq("type", 1);
        List<ShopQualificationDTO> dtos = shopQualificationService.selectList(wrapper);
        return new Result<>(dtos);
    }

    @ApiOperation(value = "根据品牌ID获取店铺品牌资质及其类目",notes = "根据品牌ID获取店铺品牌资质及其类目")
    @PostMapping("/queryBrandQCList")
    public Result<ShopQualificationBrandVO> queryQualificationList(ShopQualificationBO bo){

        ShopQualificationBrandVO vo = shopQualificationService.queryBrandQCList(bo);

        return new Result<>(vo);
    }

    @ApiOperation(value = "根据品牌ID获取店铺品牌资质及其类目树",notes = "根据品牌ID获取店铺品牌资质及其类目树")
    @PostMapping("/queryBrandQCTree")
    public Result<ShopQualificationBrandVO> queryBrandQCTree(ShopQualificationBO bo){

        ShopQualificationBrandVO vo = shopQualificationService.queryBrandQCTree(bo);

        return new Result<>(vo);
    }

    @ApiOperation(value = "店铺资质表列表",notes = "店铺资质表列表")
    @PostMapping("/queryPageList")
    public Result<Page<ShopQualificationDTO>> queryPageList(ShopQualificationBO bo){
        Wrapper<ShopQualificationDTO> wrapper = new EntityWrapper<>();


        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<ShopQualificationDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = shopQualificationService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}
